// app.js
App({
  globalData: {
    SESSION_KEY_LOGIN_USER: 'SESSION_KEY_LOGIN_USER',
    playingMusicId: '',
    isPlaying: false,
    playType: 1
  },
 
  setPlayMusicId(musicId) {
    this.globalData.playingMusicId = musicId
  },
  
  getPlayMusicId() {
    return this.globalData.playingMusicId;
  },

  setPlayType(playType) {
    this.globalData.playType = playType
  },
  
  getPlayType() {
    return this.globalData.playType;
  },

  setIsPlaying(isPlaying) {
    this.globalData.isPlaying = isPlaying
  },
  
  getIsPlaying() {
    return this.globalData.isPlaying;
  },
  
  // 设置监听器
  watch: function (ctx, obj) {
    Object.keys(obj).forEach(key => {
      this.observer(ctx.data, key, ctx.data[key], function (value) {
        obj[key].call(ctx, value)
      })
    })
  },
  // 监听属性，并执行监听函数
  observer: function (data, key, val, fn) {
    Object.defineProperty(data, key, {
      configurable: true,
      enumerable: true,
      get: function () {
        return val
      },
      set: function (newVal) {
        if (newVal === val) return
        fn && fn(newVal)
        val = newVal
      },
    })
  }
})
